package org.zhigong.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.zhigong.dto.SysResult;
import org.zhigong.dto.SysSearch;
import org.zhigong.entity.Menu;
import org.zhigong.entity.Role;
import org.zhigong.entity.Page;
import org.zhigong.service.RoleService;

import java.util.List;

import static org.zhigong.util.CommonUtil.isEmpty;
import static org.zhigong.util.CommonUtil.isNumeric;


@Controller
public class RoleController {

    @Autowired
    private RoleService roleService;

    // 只做跳转用,初始化使用json获取数据处理.
    @RequestMapping(value = "/roles", method = RequestMethod.GET)
    public String init(){
        return "role";
    }

    // 分页查询获取列表,返回json
    @RequestMapping(
            value = "/roles",
            method = RequestMethod.GET,
            produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public SysResult<List<Role>> search(SysSearch sysSearch){
        SysResult<List<Role>> sysResult;

        if(sysSearch.getPageNumber() < 0){
            try{
                List<Role> roleList = roleService.getRoleListAll();
                sysResult = new SysResult<List<Role>>(true, roleList);
            }catch (Exception e){
                sysResult = new SysResult<List<Role>>(false, e.getMessage());
            }
        } else{
            Role role = new Role();
            String condS = sysSearch.getCondition();
            role.setName(condS);
            role.getPage().setCurrentPage(sysSearch.getCurrentPage());
            role.getPage().setPageNumber(sysSearch.getPageNumber());

            try{
                List<Role> roleList = roleService.getRoleList(role);
                if(roleList.size() > 0){
                    roleList.get(0).setPage(role.getPage());
                }
                sysResult = new SysResult<List<Role>>(true, roleList);
            }catch (Exception e){
                sysResult = new SysResult<List<Role>>(false, e.getMessage());
            }
        }
        return sysResult;
    }

    // 新增
    @RequestMapping(
            value = "/roles",
            method = RequestMethod.POST,
            produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public SysResult<Integer> create(@RequestBody Role role){
        SysResult<Integer> sysResult;
        try{
            Boolean add  = roleService.addRole(role);
            sysResult = new SysResult<Integer>(true, add ? 1 : 0);
        }catch (Exception e){
            sysResult = new SysResult<Integer>(false, e.getMessage());
        }
        return sysResult;
    }


    // 删除
    @RequestMapping(
            value = "/roles/{id}",
            method = RequestMethod.DELETE,
            produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public SysResult<Integer> delete(@PathVariable("id") Long id){
        SysResult<Integer> sysResult;
        try{
            Boolean del = roleService.deleteRole(id);
            sysResult = new SysResult<Integer>(true, del ? 1 : 0);
        }catch(Exception e){
            sysResult = new SysResult<Integer>(false, e.getMessage());
        }
        return sysResult;
    }

    // 更新
    @RequestMapping(
            value = "/roles/{id}",
            method = RequestMethod.PUT,
            produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public SysResult<Integer> update(@RequestBody Role role){
        SysResult<Integer> sysResult;
        try{
            Boolean upd = roleService.updateRole(role);
            sysResult = new SysResult<Integer>(true, upd ? 1 : 0);
        }catch(Exception e){
            sysResult = new SysResult<Integer>(false, e.getMessage());
        }
        return sysResult;
    }

    // 查看
    @RequestMapping(
            value = "/roles/{id}",
            method = RequestMethod.GET,
            produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public SysResult<Role> view(@PathVariable("id") Long id){
        SysResult<Role> sysResult;
        try{
            Role role = roleService.getRoleById(id);
            sysResult = new SysResult<Role>(true, role);
        }catch(Exception e){
            sysResult = new SysResult<Role>(false, e.getMessage());
        }
        return sysResult;
    }

    // 获取当前角色未选中的菜单
    @RequestMapping(
            value = "/roles/unselectedmenus/{id}",
            method = RequestMethod.GET,
            produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public SysResult<List<Menu>> getUnselectedMenu(@PathVariable("id") Long id){
        SysResult<List<Menu>> sysResult;
        try{
            List<Menu> list = roleService.getUnselectedMenu(id);
            sysResult = new SysResult<List<Menu>>(true, list);
        }catch(Exception e){
            sysResult = new SysResult<List<Menu>>(false, e.getMessage());
        }
        return sysResult;
    }
}
